var haveRefProjectStore = null;
var haveRefPremisesStore = null;
var haveRefBuildingStore = null;
var haveRefUnitStore = null;
var haveRefRoomStore = null;

Ext.define('component.marketing.haveRefund', {
	extend : 'Ext.form.Panel',
	initComponent:function(){
		
		//项目
		haveRefProjectStore = new Ext.data.Store({
	        singleton:true,
	        autoDestroy:true,
	        fields: ["id", "projectName"],
	        proxy:{
	            type:'ajax',
	            url:contextPath+'/estate/project/findProjectList?needAuth=T&resourcesCode=menu_0003_0001',
	            reader:{
                   type: 'json', 
                   root: 'root'  //数据
              }
	        } ,
	        autoLoad: false
		});
		haveRefProjectStore.load({
			callback:function(records, options, success){
				var count = haveRefProjectStore.getCount();
				if(count>0){
					var id = haveRefProjectStore.getAt(0).get("id");
					Ext.getCmp('haveRefProjectStore').setValue(id)
				}
				if(Ext.Array.contains(idata,"fin:list:all")){
					var data = {"projectName": "全部", "id": '-1'};
					var rs = Ext.create(data);
					haveRefProjectStore.insert(count+1, rs);
				}
			}
		});
		//组团
		haveRefPremisesStore = new Ext.data.Store({
	        singleton:true,
	        autoDestroy:true,
	        fields: ["id", "groupName"],
	        proxy:{
	   			type:'ajax',
	            url:contextPath+'/estate/projectPremises/findPremisesList',
	            reader:{
                   type: 'json', 
                   root: 'root'  //数据
              	}
	        } ,
	        autoLoad: false
		});
		
		haveRefPremisesStore.on("beforeload", function(){
			haveRefPremisesStore.removeAll();
			haveRefBuildingStore.removeAll();
			haveRefUnitStore.removeAll();
			haveRefRoomStore.removeAll();
			Ext.getCmp("haveRefPremisesStore").setValue("");
			Ext.getCmp("haveRefBuildingStore").setValue("")
			Ext.getCmp("haveRefUnitStore").setValue("")
			Ext.getCmp("haveRefRoomStore").setValue("")
			var projectId = Ext.getCmp("haveRefProjectStore").getValue();
			if("-1" == projectId){
				projectId = "";
			}
			haveRefPremisesStore.proxy.extraParams.projectId = projectId;
		});
		//楼栋
		haveRefBuildingStore = new Ext.data.Store({
	        singleton:true,
	        autoDestroy:true,
	        fields: ["id", "buildingName"],
	        proxy:{
	   			type:'ajax',
	            url:contextPath+'/estate/projectBuilding/findBuildingList',
	            reader:{
                   type: 'json', 
                   root: 'root'  //数据
              	}
	        } ,
	        autoLoad: false
		});
		haveRefBuildingStore.on("beforeload", function(){
			haveRefBuildingStore.removeAll();
			haveRefUnitStore.removeAll();
			haveRefRoomStore.removeAll();
			Ext.getCmp("haveRefBuildingStore").setValue("")
			Ext.getCmp("haveRefUnitStore").setValue("")
			Ext.getCmp("haveRefRoomStore").setValue("")
			var projectId = Ext.getCmp("haveRefProjectStore").getValue();
			if("-1" == projectId){
				projectId = "";
			}
			var premisesId = Ext.getCmp("haveRefPremisesStore").getValue();
			haveRefBuildingStore.proxy.extraParams.projectId = projectId;
			haveRefBuildingStore.proxy.extraParams.premisesId = premisesId;
		});
		//单元
		haveRefUnitStore = new Ext.data.Store({
	        singleton:true,
	        autoDestroy:true,
	        fields: ["id", "unitName"],
	        proxy:{
	   			type:'ajax',
	            url:contextPath+'/estate/projectUnit/findUnitList',
	            reader:{
                   type: 'json', 
                   root: 'root'  //数据
              	}
	        } ,
	        autoLoad: false
		});
		haveRefUnitStore.on("beforeload", function(){
			haveRefUnitStore.removeAll();
			haveRefRoomStore.removeAll();
			Ext.getCmp("haveRefUnitStore").setValue("")
			Ext.getCmp("haveRefRoomStore").setValue("")
			var projectId = Ext.getCmp("haveRefProjectStore").getValue();
			if("-1" == projectId){
				projectId = "";
			}
			var premisesId = Ext.getCmp("haveRefPremisesStore").getValue();
			var buildingId = Ext.getCmp("haveRefBuildingStore").getValue();
			haveRefUnitStore.proxy.extraParams.projectId = projectId;
			haveRefUnitStore.proxy.extraParams.premisesId = premisesId;
			haveRefUnitStore.proxy.extraParams.buildingId = buildingId;
		});
		//房间
		haveRefRoomStore = new Ext.data.Store({
	        singleton:true,
	        autoDestroy:true,
	        fields: ["id", "roomName"],
	        proxy:{
	   			type:'ajax',
	            url:contextPath+'/estate/projectRoom/findRoomList',
	            reader:{
                   type: 'json', 
                   root: 'root'  //数据
              	}
	        } ,
	        autoLoad: false
		});
		haveRefRoomStore.on("beforeload", function(){
			haveRefRoomStore.removeAll();
			Ext.getCmp("haveRefRoomStore").setValue("")
			var projectId = Ext.getCmp("haveRefProjectStore").getValue();
			if("-1" == projectId){
				projectId = "";
			}
			var premisesId = Ext.getCmp("haveRefPremisesStore").getValue();
			var buildingId = Ext.getCmp("haveRefBuildingStore").getValue();
			var unitId = Ext.getCmp("haveRefUnitStore").getValue();
			haveRefRoomStore.proxy.extraParams.projectId = projectId;
			haveRefRoomStore.proxy.extraParams.premisesId = premisesId;
			haveRefRoomStore.proxy.extraParams.buildingId = buildingId;
			haveRefRoomStore.proxy.extraParams.unitId = unitId;
		});
		
		//房款类型
	    var typeHaveRefund = [
	         {"value":"","text":"请选择"},
	         {"value":"2","text":"退认筹金"},
	         {"value":"4","text":"退认购金"},
	         {"value":"21","text":"退签约金"},
	         {"value":"34","text":"退首付分期"},
	         {"value":"23","text":"补差款"},
	         {"value":"24","text":"手续费"}
	    ];
	    if(payDepositOff){
	    	typeHaveRefund=typeHaveRefund.del(1);
	    }
	    var relationTypeHaveRefund = Ext.data.Store({
	    	autoDestroy: true,
	        fields :["value","text"],
	        data : typeHaveRefund
	    });
	    
	    //房号list
	    var roomList = new Ext.data.Store({
	        singleton:true,
	        autoDestroy:true,
	        fields: ["combinedName", "id"],
	        proxy:{
	            type:'ajax',
	            url:jypath+'/marketing/haveReceiveRefund/findRoomId',
	            reader:{
                    type: 'json', 
                    root: 'root'  //数据
               }
	        },
	        autoLoad:true
        });
	    
	    //员工list
	    var haveRefUserList = new Ext.data.Store({
	        fields: ["userName", "id"],
	        proxy:{
	            type:'ajax',
	            url:jypath+'/customer/customer/userByProjectIdForGroup',
	            reader:{
                    type: 'json', 
                    root: 'root'  //数据
               }
	        },
	        autoLoad: false
        });
	    
	    haveRefUserList.on("beforeload", function(){
			var projectId = Ext.getCmp("haveRefProjectStore").getValue();
			if("-1" == projectId){
				projectId = "";
				return false;
			}
			haveRefUserList.proxy.extraParams.projectId = projectId;
		})
		
		var formHaveRefund = Ext.create("Ext.form.Panel", {
		    title: "查询条件",
		    collapsible: true,  //可折叠
		    autoScroll: false,   //自动创建滚动条
		    defaultType: 'textfield',
		    defaults: {
		    	 anchor: '100%'
		    },
		    fieldDefaults: {
		        margin: 2,
		        labelWidth: 80,
		        labelAlign: "right",
		        margin: "5 0 0 0"
		    },
		    items: [{
			    xtype: "container",
			    layout: "hbox",
			    items: [
					{  
			            xtype:'combobox',
			            id:'haveRefProjectStore',
			            name: "projectId",
			            store: haveRefProjectStore,
			            valueField: "id",  
			            displayField: "projectName",
			            fieldLabel:'所属项目',
			            queryMode:'local',
			            typeAhead: true,
			            listeners:{
			            	'change':function(value){
			            		//加载组团数据
			            		if(value.lastValue && value.lastValue != "-1"){
			            			haveRefUserList.load();
			            			haveRefPremisesStore.load({
			            				callback: function(records, operation, success){
			            					//若无组团数据，加载楼栋
			            					var recordLen = records.length;
			            					if(recordLen == 0){
			            						haveRefBuildingStore.load();
			            					}
			            				}
			            			});
			            		}else{
			            			haveRefUserList.removeAll();
			            			haveRefPremisesStore.removeAll();
			            			haveRefBuildingStore.removeAll();
			            			haveRefUnitStore.removeAll();
			            			haveRefRoomStore.removeAll();
			            			Ext.getCmp("haveRefPremisesStore").setValue("");
			            			Ext.getCmp("haveRefBuildingStore").setValue("")
			            			Ext.getCmp("haveRefUnitStore").setValue("")
			            			Ext.getCmp("haveRefRoomStore").setValue("")
			            		}
			            	}
			            }
			        },
					{  
			            xtype:'combobox',
			            id:'haveRefPremisesStore',
			            name: "premisesId",
			            store: haveRefPremisesStore,
			            valueField: "id",  
			            displayField: "groupName",
			            queryMode:'local',
			            typeAhead: true,
			            listeners:{
			            	'change':function(value){
			            		//加载楼栋
			            		//若无楼栋数据，加载单元
			            		if(value.lastValue && value.lastValue != "-1"){
			            			haveRefBuildingStore.load({
			            				callback: function(records, operation, success){
			            					//若无组团数据，加载楼栋
			            					var recordLen = records.length;
			            					if(recordLen == 0){
			            						haveRefUnitStore.load();
			            					}
			            				}
			            			});
			            		}
			            	}
			            }
			        },
					{  
			            xtype:'combobox',
			            id:'haveRefBuildingStore',
			            name: "buildingId",
			            store: haveRefBuildingStore,
			            valueField: "id",  
			            displayField: "buildingName",
			            queryMode:'local',
			            typeAhead: true,
			            listeners:{
			            	'change':function(value){
			            		//加载单元
			            		//若无单元数据，加载房间
			            		if(value.lastValue && value.lastValue != "-1"){
			            			haveRefUnitStore.load({
			            				callback: function(records, operation, success){
			            					//若无组团数据，加载楼栋
			            					var recordLen = records.length;
			            					if(recordLen == 0){
			            						haveRefRoomStore.load();
			            					}
			            				}
			            			});
			            		}
			            	}
			            }
			        },
					{  
			            xtype:'combobox',
			            id:'haveRefUnitStore',
			            name: "unitId",
			            store: haveRefUnitStore,
			            valueField: "id",  
			            displayField: "unitName",
			            queryMode:'local',
			            typeAhead: true,
			            listeners:{
			            	'change':function(value){
			            		//加载房间
			            		if(value.lastValue && value.lastValue != "-1"){
			            			haveRefRoomStore.load();
			            		}
			            	}
			            }
			        },
					{  
			            xtype:'combobox',
			            id:'haveRefRoomStore',
			            name: "roomId",
			            store: haveRefRoomStore,
			            valueField: "id",  
			            displayField: "roomName",
			            queryMode:'local',
			            typeAhead: true
			        }
	        	]
			},{
		            xtype: "container",
		            layout: "hbox",
		            items: [{ xtype: "hiddenfield", name: "docType", value: "1"},
						{ 
							xtype: "checkboxgroup",
							id:"busStatusHaveRefund",
							fieldLabel: "业务类型",
							name :"receivablesType",
							width:400,
							items:[{
						    		boxLabel:'认筹',
						    		inputValue:'0'
						    		//hidden: payDepositOff
						    	},{
						    		boxLabel:'认购',
						    		inputValue:'1'
						    	},{
						    		boxLabel:'签约',
						    		inputValue:'2'
						    	},{
						    		boxLabel:'面积补差',
						    		inputValue:'3'
						    	}
						     ]
						}
		            ]
	            },{
		            xtype: "container",
		            layout: "hbox",
		            items: [
						{  
		                  xtype:'combobox',
		 		    	  fieldLabel:'房款类型',
		 		    	  store:relationTypeHaveRefund,
		 		    	  displayField: "text",
		 		    	  valueField:"value",
		 		    	  id:'relationTypeHF',
		 		    	  name:'housingFundType'
	        	        },
						{ xtype:'combobox',
			 		    	  fieldLabel:'经&nbsp;&nbsp;纪&nbsp;人',
			 		    	  store:haveRefUserList,
			 		    	  displayField: "userName",
			 		    	  valueField:"id",
			 		    	  name:'createUserId'
			 		    }
		            ]
	            },{
		            xtype: "container",
		            layout: "hbox",
		            items: [
						{ xtype: "textfield", name: "relationCode", fieldLabel: "业务编号"},
						{ xtype: "textfield", name: "customer_name", fieldLabel: "客户名称"}
		            ]
	            },{
	            	 xtype: "container",
			         layout: "hbox",
			         items:[
						{
							xtype:'container',
							layout:'hbox',
							items:[{
								xtype:'datefield',
						        fieldLabel:'开始有效日期',
						        name:'ysDateStart',
						        format: 'Y-m-d'
							},{
								xtype:'datefield',
								name:'ysDateEnd',
						        format: 'Y-m-d',
						        fieldLabel:'结束有效日期'
							}]
						}
	                ]
	            }
		        ],
		    buttons: [
	  	        { xtype: "button", text: "查询", 
	  	        	handler:function(){
	  	        		//获取通过checkboxgroup定义的checkbox值
	  	        		//业务状态
	  	        		var busStatusValue = Ext.getCmp('busStatusHaveRefund').getChecked();
	  	        		var busStatus="";
	  	        		Ext.Array.each(busStatusValue, function(item){
	  	        			busStatus += item.inputValue+',' ;
	  	        		});

	  	        		gridStoreHaveRefund.getProxy().extraParams.receivablesType = busStatus;
	  	        		gridStoreHaveRefund.load();

	  	        		var pagingTool=Ext.getCmp("pagingTool3");
	  	        		pagingTool.moveFirst();//跳至第一页
	  	        	}  
	  	        },{
	  	        	 xtype: "button",hidden:finHaveReceiveExport, text: "导出",handler:function(){
	  	        		 var count = gridHaveRefund.getStore().getCount();
	  	        		 if(count<=0){
	  	        			Ext.MessageBox.alert("系统提示","没有可导出的数据");
	  	        		 }else{
	  	        			var busStatusValue = Ext.getCmp('busStatusHaveRefund').getChecked();
		  	        		var busStatus="";
		  	        		Ext.Array.each(busStatusValue, function(item){
		  	        			busStatus += item.inputValue+',' ;
		  	        		});
		  	        		gridStoreHaveRefund.getProxy().extraParams.receivablesType = busStatus;
		  	        		var value = encodeURI(encodeURI(JSON.stringify(gridStoreHaveRefund.getProxy().extraParams),"utf-8"));
		  	        		var url = jypath + "/marketing/haveReceiveRefund/haveReceicerefundExport?json="+value;
		  	        		location.href = url;
		  	        	 }
	  	        		}
	  	        },
	  	        { xtype: "button", text: "清空",handler:function(){
	  	        	formHaveRefund.getForm().reset();
	  	        	}
	  	        }
	  	    ]    
	});
	
	var gridStoreHaveRefund = Ext.create('Ext.data.Store', {
        fields: ["customer_name", "customerFamilyName","combined_name", "calculatArea", "price", "totalPrice", "relationType",
                 "businessType", "realAmount", "billCode", "statement", "bankAccount", "reChecked", "receivablesDate"],
        pageSize: 10,  //页容量15条数据
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        proxy: {
            type: 'ajax',
            url: jypath+'/marketing/haveReceiveRefund/list?resourcesCode=menu_0003_0001',
            actionMethods: { read: 'POST' },
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'rows',  //数据
                totalProperty: 'total' //数据总条数
            }
        },
        autoLoad: true  //即时加载数据
    });
	
	gridStoreHaveRefund.on("beforeload",function(){
        Ext.apply(gridStoreHaveRefund.proxy.extraParams,formHaveRefund.getForm().getValues());
        var projectId = Ext.getCmp("haveRefProjectStore").getValue();
        if(projectId == "-1"){
        	gridStoreHaveRefund.proxy.extraParams.projectId = "";
        }
        var newResCode = "menu_0003_0001";
        gridStoreHaveRefund.proxy.extraParams.resourcesCode = newResCode;
    });
	
	function onreloadHaveRefund(){
		gridStoreHaveRefund.on("beforeload",function(){
	        Ext.apply(gridStoreHaveRefund.proxy.extraParams,formHaveRefund.getForm().getValues());
	    });
	}
	
	var gridHaveRefund = Ext.create('Ext.grid.Panel', {
		minHeight:450,
		minWidth:300,
        store: gridStoreHaveRefund,
        columnLines: true,
        columns: [{  
        	text : "操作",  
            dataIndex: 'actioncolumn',
            locked : true,
            renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
            	var rec = gridHaveRefund.getStore().getAt(rowIndex);
            	var html = "";
                //冲销
            	if(finHaveReceivewWriteOff==false){
            		if(rec.get('reChecked') != "1"){
                        html = '<a onclick="chongxiao('+rec.get('id')+')" style="cursor:pointer">冲销</a>';
                	}
            	}

                html += "&nbsp;<a href='javascript:parent.openMenu(\"0\",\"print_"+rec.get('id')+"\",\"\",\"收据打印\",\"/marketing/haveReceiveRefund/toPrint?id="+rec.get('id')+"&printFlag=1&returnUrl=,marketing,pin,index\")' style='cursor:pointer'>打印</a>";
            	return html;
        	}
        },{
            text     : '客户名称', 
            dataIndex: 'customer_name'
        },{
			text     : '共同购房者',
			dataIndex: 'customerFamilyName'
		},{
            text     : '房号',
            dataIndex: 'combined_name'
        },{
            text     : '签约面积(㎡)',
            dataIndex: 'calculatArea'
        },{
            text     : '签约单价(元)',
            dataIndex: 'price'
        },{
            text     : '签约金额(元)',
            dataIndex: 'totalPrice'
        },{
            text     : '房款类型',
            dataIndex: 'relationType'
        },{
            text     : '业务标志',
            dataIndex: 'businessType'
        },{
            text     : '发生额(元)',
            dataIndex: 'realAmount'
        },{
            text     : '票据号码',
            dataIndex: 'billCode'
        },{
            text     : '结算日期/结算人员/结算方式',
            dataIndex: 'statement'
        },{
            text     : '结算银行',
            dataIndex: 'bankAccount'
        },{
            text     : '复核状态',
            dataIndex: 'reChecked',
            renderer : function(val) {
            	var html = "";
            	if(val == "0" || val == ""){
            		html = "未复核";
            	}else if(val == "1"){
            		html = "已复核";
            	}
            	return html;
            }
        },{
            text     : '操作日期',
            dataIndex: 'receivablesDate',
            renderer : function(val) {
            	return dateFormat(val);
            }
        }],
        title: '已退款明细列表',
        dockedItems: [{  
            xtype: 'pagingtoolbar',  
            store: gridStoreHaveRefund,   // GridPanel中使用的数据  
            dock: 'bottom',  
            displayInfo: true,  
            emptyMsg:"没有数据",  
            displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据",
            id:'pagingTool3'
        }
        ]
    });
	
		
		var defaultConfigHaveRefund = {
				defaults: {  
		            split: true,    //有分割线  
		            collapsible: true  //可以折叠
		        },
				items:[formHaveRefund,gridHaveRefund]
		};
		Ext.apply(this,defaultConfigHaveRefund);
		this.callParent(arguments);
		
	}
});
